﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

public partial class gestionComercial_impresionSeparacion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String pValor = Request.QueryString["idSeparacion"];
        hdfImprimir.Value = pValor;
        String pIdProforma = Request.QueryString["idProyecto"];
        hdfIdProyecto.Value = pIdProforma;
        String pIdEtapa = Request.QueryString["idEtapa"];
        hdfIdEtapa.Value = pIdEtapa;
        String pDescEmpresa = Request.QueryString["descEmpresa"];
        hdfEmpresa.Value = pDescEmpresa;
    }
    private void mostrarlink()
    {
        XmlDocument xDoc = new XmlDocument();
        string Empresa = hdfEmpresa.Value;
        //La ruta del documento XML permite rutas relativas 
        //respecto del ejecutable!
        //string archivoXML = Server.MapPath("~/gestionComercial/EmpresaPermiso.xml");
        xDoc.Load(Server.MapPath("~/gestionComercial/EmpresaPermiso.xml"));
        XmlNodeList empresas = xDoc.GetElementsByTagName("Empresas");
        XmlNodeList lista = ((XmlElement)empresas[0]).GetElementsByTagName("Empresa");
        foreach (XmlElement nodo in lista)
        {
            int i = 0;
            XmlNodeList nNombre = nodo.GetElementsByTagName("NombreEmpresa");
            XmlNodeList nPermiso = nodo.GetElementsByTagName("TipoPermiso");
            if (nNombre[i].InnerText == Empresa.ToUpper())
            {
                if (nPermiso[i].InnerText == "0")
                {
                    lnbImprimirWord.Visible = false;
                }
            }
        }
    }
    protected void lnbImprimir_Click(object sender, EventArgs e)
    {
        String empresa = Convert.ToString(hdfEmpresa.Value);

        //String strConnection = "Provider=SQLNCLI;Server=SERVIDOR;Database=EMPRESA;Uid=USUARIO; Pwd=CONTRASEÑA;";
        String strConnection = ConfigurationManager.ConnectionStrings[empresa.ToUpper()].ConnectionString;

        //MPE OleDbConnection Connection = new OleDbConnection(strConnection);
        SqlConnection Connection = new SqlConnection(strConnection);

        //MPE OleDbDataAdapter DA = new OleDbDataAdapter("usp_venta_rptVenta", Connection);
        SqlDataAdapter DA = new SqlDataAdapter("usp_venta_rptVenta", Connection);

        DA.SelectCommand.CommandType = CommandType.StoredProcedure;
        DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
        DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
        DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
        DataSet DS = new DataSet();
        DA.Fill(DS, "T0");

        String wModelo = "";

        switch (empresa.ToLower()){
            case "ejjc":
                foreach (DataRow Proforma in DS.Tables[0].Rows)
                {
                    String ModeloReporte = Convert.ToString(Proforma["idTpoMinuta"]);
                    if (ModeloReporte != "") {
                        if (ModeloReporte != "0") {
                        wModelo = "M" + ModeloReporte;
                        }

                    }
                }
        break;
        }

        switch (empresa.ToLower())
        {
            case "ecaral":
                foreach (DataRow Proforma in DS.Tables[0].Rows)
                {
                    String ModeloReporte = Convert.ToString(Proforma["idTpoInmueble"]);
                    if (ModeloReporte != "")
                    {
                        if (ModeloReporte != "0")
                        {
                            wModelo = "M" + ModeloReporte;
                        }

                    }
                }
                break;
        }

        switch (empresa.ToLower())
        {
            case "ellaxta":
                DA = new SqlDataAdapter("usp_separacion_rptSeparacionPagosSaldoFinanciar", Connection);
                DA.SelectCommand.CommandType = CommandType.StoredProcedure;
                DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
                DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
                DA.Fill(DS, "T1");
                break;
        }
        
        String repFilePath = null;
        repFilePath = Server.MapPath("~/gestionComercial/rptVenta/venta" + empresa.ToString() + hdfIdProyecto.Value.ToString() + wModelo + ".rpt");
        if (System.IO.File.Exists(repFilePath)){
            ReportDocument repDoc1 = new ReportDocument();
            repDoc1.Load(repFilePath);
            repDoc1.SetDataSource(DS.Tables["T0"]);

            ReportDocument repDoc = repDoc1;
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            switch (empresa.ToLower())
            {
                case "ellaxta":
                    SubreportObject subReportObject;
                    ReportDocument subReporte;
                    subReportObject = repDoc1.ReportDefinition.ReportObjects["Subreport1"] as SubreportObject;
                    subReporte = repDoc1.OpenSubreport(subReportObject.SubreportName);
                    subReporte.SetDataSource(DS.Tables["T1"]);
                    break;
            }
            try
            {
                repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Venta");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ex = null;
            }
        }       
    }

    //protected void lnbImprimirWord_Click(object sender, EventArgs e)
    //{
    //    String empresa = Convert.ToString(hdfEmpresa.Value);

    //    //String strConnection = "Provider=SQLNCLI;Server=SERVIDOR;Database=EMPRESA;Uid=USUARIO; Pwd=CONTRASEÑA;";
    //    String strConnection = ConfigurationManager.ConnectionStrings[empresa.ToUpper()].ConnectionString;

    //    //MPE OleDbConnection Connection = new OleDbConnection(strConnection);
    //    SqlConnection Connection = new SqlConnection(strConnection);

    //    //MPE OleDbDataAdapter DA = new OleDbDataAdapter("usp_venta_rptVenta", Connection);
    //    SqlDataAdapter DA = new SqlDataAdapter("usp_venta_rptVenta", Connection);

    //    DA.SelectCommand.CommandType = CommandType.StoredProcedure;
    //    DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
    //    DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
    //    DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
    //    DataSet DS = new DataSet();
    //    DA.Fill(DS, "T0");

    //    String repFilePath = null;
    //    repFilePath = Server.MapPath("~/gestionComercial/rptVenta/venta" + empresa.ToString() + hdfIdProyecto.Value.ToString() + ".rpt");
    //    if (System.IO.File.Exists(repFilePath))
    //    {
    //        ReportDocument repDoc1 = new ReportDocument();
    //        repDoc1.Load(repFilePath);
    //        repDoc1.SetDataSource(DS.Tables["T0"]);

    //        ReportDocument repDoc = repDoc1;
    //        Response.Buffer = false;
    //        Response.ClearContent();
    //        Response.ClearHeaders();

    //        try
    //        {
    //            repDoc.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, "Venta");
    //        }
    //        catch (Exception ex)
    //        {
    //            Console.WriteLine(ex.Message);
    //            ex = null;
    //        }
    //    }
    //}

    protected void lnbImprimirWord_Click(object sender, EventArgs e)
    {
        String empresa = Convert.ToString(hdfEmpresa.Value);

        //String strConnection = "Provider=SQLNCLI;Server=SERVIDOR;Database=EMPRESA;Uid=USUARIO; Pwd=CONTRASEÑA;";
        String strConnection = ConfigurationManager.ConnectionStrings[empresa.ToUpper()].ConnectionString;

        //MPE OleDbConnection Connection = new OleDbConnection(strConnection);
        SqlConnection Connection = new SqlConnection(strConnection);

        //MPE OleDbDataAdapter DA = new OleDbDataAdapter("usp_venta_rptVenta", Connection);
        SqlDataAdapter DA = new SqlDataAdapter("usp_venta_rptVenta", Connection);

        DA.SelectCommand.CommandType = CommandType.StoredProcedure;
        DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
        DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
        DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
        DataSet DS = new DataSet();
        DA.Fill(DS, "T0");

        String wModelo = "";

        switch (empresa.ToLower())
        {
            case "ejjc":
                foreach (DataRow Proforma in DS.Tables[0].Rows)
                {
                    String ModeloReporte = Convert.ToString(Proforma["idTpoMinuta"]);
                    if (ModeloReporte != "")
                    {
                        if (ModeloReporte != "0")
                        {
                            wModelo = "M" + ModeloReporte;
                        }

                    }
                }
            break;
        }

        switch (empresa.ToLower())
        {
            case "ecaral":
                foreach (DataRow Proforma in DS.Tables[0].Rows)
                {
                    String ModeloReporte = Convert.ToString(Proforma["idTpoInmueble"]);
                    if (ModeloReporte != "")
                    {
                        if (ModeloReporte != "0")
                        {
                            wModelo = "M" + ModeloReporte;
                        }

                    }
                }
                break;
        }

        switch (empresa.ToLower())
        {
            case "ellaxta":
                DA = new SqlDataAdapter("usp_separacion_rptSeparacionPagosSaldoFinanciar", Connection);
                DA.SelectCommand.CommandType = CommandType.StoredProcedure;
                DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
                DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
                DA.Fill(DS, "T1");
                break;
        }

        String repFilePath = null;
        repFilePath = Server.MapPath("~/gestionComercial/rptVenta/venta" + empresa.ToString() + hdfIdProyecto.Value.ToString() + wModelo + ".rpt");
        if (System.IO.File.Exists(repFilePath))
        {
            ReportDocument repDoc1 = new ReportDocument();
            repDoc1.Load(repFilePath);
            repDoc1.SetDataSource(DS.Tables["T0"]);

            ReportDocument repDoc = repDoc1;
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            switch (empresa.ToLower())
            {
                case "ellaxta":
                    SubreportObject subReportObject;
                    ReportDocument subReporte;
                    subReportObject = repDoc1.ReportDefinition.ReportObjects["Subreport1"] as SubreportObject;
                    subReporte = repDoc1.OpenSubreport(subReportObject.SubreportName);
                    subReporte.SetDataSource(DS.Tables["T1"]);
                    break;
            }
            try
            {
                repDoc.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, "Venta");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ex = null;
            }
        }
    }
}
